package aladyn;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.StringReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.StringTokenizer;

import aladyn.exceptions.LRGenerationException;
import aladyn.generator.LRSerializableGenerator;
import aladyn.testclass.Pixel;
import aladyn.xmlparser.LRSerializableXmlHandler;
import aladyn.xmlparser.LRSerializableXmlParser;
//import java.io.FileReader;

import javassist.*;

public class Main {
	
	  static final String DONNEES_XML =
		  "<?xml version=\"1.0\"?>\n"
		  + "<class_list jar=\"geometryClasses\">"
		  + 	"<class path=\"aladyn.testclass.Dot\"/>"
		  + 	"<class path=\"aladyn.testclass.Pixel\"/>"
		  + 	"<class path=\"aladyn.testclass.Point\"/>" 
		  +	"</class_list>";

	  static final String fileName = "File\\classes.xml";
	
	  
	/**
	 * @param args
	 * @throws FileNotFoundException 
	 */
	public static void main(String[] args) throws FileNotFoundException {
		
		ClassPool pool = ClassPool.getDefault();
		try {
			/*
			CtClass point = pool.get("aladyn.testclass.Pixel");
			//CtClass inter = pool.get("aladyn.interfaces.LRSerializable");
			LRSerializableGenerator.addLRSerializableInterface(point);
			
			CtMethod[] methods = point.getMethods();
			for(CtMethod method : methods)
				System.out.println(method);
			
			Class pixel = point.toClass();
			Pixel p = (Pixel)pixel.newInstance();
			Pixel p2 = new Pixel(1, 4);
			Method m;
			for(Method method : pixel.getMethods())
				if(method.getName().equals("serialize")){
					m = method;
					System.out.println(m.invoke(p, new Object[0]));
					System.out.println(m.invoke(p2, new Object[0]));
				}else if(method.getName().equals("deserialize")){
					m = method;
					Object[] tab = new Object[1];
					tab[0] = new StringTokenizer("aladyn.testclass.Pixel@1@4", "@");
					System.out.println(m);
					//System.out.println(m.invoke(p, tab));
					
				}
			System.out.println("\n\n\n");
			*/
			
			
			//MyXmlParser.parse(new StringReader(DONNEES_XML));
			ArrayList<String> classes = LRSerializableXmlParser.parse(new FileReader(fileName));
			for(String str : classes){
				System.out.println(str);
				LRSerializableGenerator.addLRSerializable(str);
			}
			
			LRSerializableGenerator.generateTypeMethodString(classes);
			LRSerializableGenerator.generateTypeMethodStringStringTokenizer(classes);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
			
	}

}
